package com.everest.tribalapp;


import android.app.Activity;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.telephony.PhoneStateListener;
import android.telephony.ServiceState;
import android.telephony.SmsManager;
import android.telephony.TelephonyManager;
import android.widget.RadioButton;
import android.widget.Toast;

public class SmsSendFunction1 extends Activity{
	public static final String SIM_ABSENT = "Absent";
    public static final String SIM_READY = "Ready";
    public static final String SIM_PIN_REQUIRED = "PIN required";
    public static final String SIM_PUK_REQUIRED = "PUK required";
    public static final String SIM_NETWORK_LOCKED = "Network locked";
    public static final String SIM_UNKNOWN = "Unknown";
	private String sms;
	private String smsno;
	private String state2;
	private String detail;
	private String user;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		setContentView(R.layout.dum);
		Intent i=getIntent();
		 detail=i.getStringExtra("detail");
		 user=Global.username;
		final TelephonyManager tm = (TelephonyManager) getSystemService(TELEPHONY_SERVICE);
		state2 = updateViews(tm);
        tm.listen(new PhoneStateListener()
        {
          @Override
           public void onServiceStateChanged(ServiceState serviceState)
          {
           	 state2 = updateViews(tm);
           }
           public void onDataConnectionStateChanged(String state) 
           {
           	 state2 = updateViews(tm);
           }

       }, PhoneStateListener.LISTEN_SERVICE_STATE | PhoneStateListener.LISTEN_DATA_CONNECTION_STATE);
      
		if(!state2.equalsIgnoreCase("Absent"))
		{	
			
			sms(Global.phone_no);
		       
        }
       	else
       	{
       		
			Toast.makeText(getApplicationContext(),"Sim Network Absent..Sim Failed..!", Toast.LENGTH_SHORT).show();
			
				Intent intent=new Intent(SmsSendFunction1.this,Home.class);
				startActivity(intent);
				SmsSendFunction1.this.finish();
		
		
       	}
	}
		
	
	void sms(String smsno)
	{
		sms="**This message has been recieved from user="+user+" Survey Desk App** "+"the details are "+detail;
		SmsManager smsManager = SmsManager.getDefault();
		smsManager.sendTextMessage(smsno, null, sms, null, null);
		
			Intent intent=new Intent(SmsSendFunction1.this,Home.class);
			startActivity(intent);
			SmsSendFunction1.this.finish();
		
	}
	
	
	private final String updateViews(TelephonyManager tm) 
	{

        String state1 = mapSimStateToName(tm.getSimState());
        return state1;
    }
	private static String mapSimStateToName(int simState)
	{
        switch (simState) {
            case TelephonyManager.SIM_STATE_ABSENT:
                return SIM_ABSENT;
            case TelephonyManager.SIM_STATE_READY:
                return SIM_READY;
            case TelephonyManager.SIM_STATE_PIN_REQUIRED:
                return SIM_PIN_REQUIRED;
            case TelephonyManager.SIM_STATE_PUK_REQUIRED:
                return SIM_PUK_REQUIRED;
            case TelephonyManager.SIM_STATE_NETWORK_LOCKED:
                return SIM_NETWORK_LOCKED;
            case TelephonyManager.SIM_STATE_UNKNOWN:
                return SIM_UNKNOWN;
            default:
                // shouldn't happen.
                return null;
        }
    }
	@Override
	protected void onDestroy() 
	{
		// TODO Auto-generated method stub
		super.onDestroy();
	}

	@Override
	public void onBackPressed()
	{
		
	}

}
